import 'package:go_router/go_router.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:zentoon_flutter/screens/free_list_screen.dart';
import 'package:zentoon_flutter/screens/reder/comic_reader_screen.dart';
import 'package:zentoon_flutter/screens/search_index.dart';
import '../screens/home/home_detail_screen.dart';
import '../screens/main_screen.dart';
import '../screens/comic_detail_screen.dart';
// import '../screens/comic_reader_screen.dart';
import '../screens/feedback_screen.dart';
import '../screens/manhua_list_screen.dart';
import '../screens/mine/settings_screen.dart';
import '../screens/premium_screen.dart';
import '../screens/splash_screen.dart'; // 添加启动页导入

// import '../screens/mine/settings_screen.dart';
import '../screens/mine/about_screen.dart';
import '../screens/mine/topup_screen.dart';

import '../screens/daily_checkin_screen.dart';
import '../screens/reder/comic_reader_page.dart';
import '../utils/navigation_service.dart';

final appRouterProvider = Provider<GoRouter>((ref) {
  return GoRouter(
    initialLocation: '/splash', // 修改初始路由为启动页
    navigatorKey: NavigationService.navigatorKey, // 绑定全局 NavigatorKey
    routes: [
      // 添加启动页路由
      GoRoute(
        path: '/splash',
        name: 'splash',
        builder: (context, state) => const SplashScreen(),
      ),
      GoRoute(
        path: '/',
        name: 'home',
        builder: (context, state) => const MainScreen(),
      ),
      GoRoute(
        path: '/comic/:id',
        name: 'comic_detail',
        builder: (context, state) {
          final comicId = state.pathParameters['id']!;
          return ComicDetailScreen(comicId: comicId);
        },
      ),
      GoRoute(
        path: '/reader/:chapterId',
        name: 'comic_reader',
        builder: (context, state) {
          final chapterId = state.pathParameters['chapterId']!;
          final comicId = state.uri.queryParameters['comicId'];
          final chapterIdsString = state.uri.queryParameters['chapterIds'];
          final chapterIds = chapterIdsString?.split(',');

          return ComicReaderScreen(
            chapterId: chapterId,
            comicId: comicId,
            chapterIds: chapterIds,
          );
        },
      ),
      // GoRoute(
      //   path: '/comic_reader/:chapterId', // 更改路径
      //   name: 'comic_reader_new', // 更改名称
      //   builder: (context, state) {
      //     final chapterId = state.pathParameters['chapterId']!;
      //     final comicDetail = state.extra as ComicDetailModel;
      //
      //     return ComicReaderPage(
      //       detail: comicDetail,
      //       initialChapterId: chapterId,
      //     );
      //   },
      // ),

      GoRoute(
        path: '/feedback',
        name: 'feedback',
        builder: (context, state) => const FeedbackScreen(),
      ),
      GoRoute(
        path: '/settings',
        name: 'settings',
        builder: (context, state) => const SettingsScreen(),
      ),
      GoRoute(
        path: '/about',
        name: 'about',
        builder: (context, state) => const AboutScreen(),
      ),
      // 添加TopUpScreen
      GoRoute(
        path: '/topup',
        name: 'topup',
        builder: (context, state) => const TopUpScreen(),
      ),
      GoRoute(
        path: '/daily-checkin',
        name: 'daily-checkin',
        builder: (context, state) => const DailyCheckInScreen(),
      ),
      GoRoute(
        path: '/free_page',
        name: 'free_page',
        builder: (context, state) => const FreeScreen(),
      ),
      // 这里声明
      GoRoute(
        path: '/manhua_list/:categoryId/:sortBy/:title',
        name: 'manhua_list',
        builder: (context, state) {
          final categoryId = state.pathParameters['categoryId']!;
          final sortBy = int.parse(state.pathParameters['sortBy']!);
          final title = state.pathParameters['title']!;
          return ManhuaListScrren(
            categoryId: categoryId,
            sortBy: sortBy,
            title: title,
          );
        },
      ),

      GoRoute(
        path: '/search',
        name: 'search',
        builder: (context, state) => const SearchBarScreen(),
      ),

      GoRoute(
        path: '/home_detail/:comicId',
        name: 'home_detail',
        builder: (context, state) {
          final comicId = state.pathParameters['comicId']!;
          return ComicDetailPage(comicId: comicId);
        },
      ),
      GoRoute(
        path: '/PremiumScreen',
        name: 'PremiumScreen',
        builder: (context, state) {
          return const PremiumScreen();
        },
      ),
      GoRoute(
        path: '/direct_reader/:chapterId/:comicId',
        name: 'direct_reader',
        builder: (context, state) {
          final chapterId = state.pathParameters['chapterId']!;
          final comicId = state.pathParameters['comicId']!;
          print("sadsa = $chapterId, $comicId");
          return ComicReaderPage(
            initialChapterId: comicId,
            selectedChapterId: chapterId,
          );
        },
      ),
    ],
  );
});
